package com.yegs.jltqc.serviceImpl;

import com.yegs.jltqc.TO.NamePageQuery;
import com.yegs.jltqc.TO.QualityPageQuery;
import com.yegs.jltqc.U8Service.U8InComeService;
import com.yegs.jltqc.VO.Quality;
import com.yegs.jltqc.VO.RpXunjian;
import com.yegs.jltqc.common.StoreException;
import com.yegs.jltqc.config.ConcurentCache;
import com.yegs.jltqc.dao.QualityRepository;
import com.yegs.jltqc.service.QualityService;
import com.yegs.jltqc.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class QualityServiceImpl implements QualityService {

    @Autowired
    private QualityRepository qualityRepository;
    @Autowired
    private U8InComeService u8InComeService;

    @Override
    public Quality save(Quality quality) {
        quality.setCreateTime(new Date());
        quality.setCreateBy( ConcurentCache.getUserName());
        u8InComeService.finishByAutoid(quality.getAutoid());
        return qualityRepository.save(quality);
    }

    @Override
    public List<Quality> list() {
        return qualityRepository.findAll();
    }

    @Override
    public Long getNext(Long id) {
        if(id == null){
            Quality project = qualityRepository.findFirstByOrderByIdDesc();
            if(project != null) {
                return project.getId();
            }
        }
        Quality project = qualityRepository.findFirstByIdIsAfter(id);
        if(project == null){ project = qualityRepository.findFirstByOrderByIdDesc(); }
        if(project != null){
            return project.getId();
        }
        return null;
    }

    @Override
    public Long getLast(Long id) {
        if(id == null){
            Quality project = qualityRepository.findFirstByOrderByIdDesc();
            if(project != null) {
                return project.getId();
            }
        }
        else{
            Quality project = qualityRepository.findFirstByIdBeforeOrderByIdDesc(id);
            if(project == null){ project = qualityRepository.findFirstByOrderById(); }
            if(project != null){
                return project.getId();
            }
        }
        return null;
    }


    @Override
    public Quality get(Long id) {
        return qualityRepository.getOne(id);
    }

    @Override
    public void del(Long id) {
        qualityRepository.deleteById(id);
    }

    @Override
    public Page<Quality> query(QualityPageQuery query) {
        Sort sort = Sort.by(Sort.Direction.DESC,"id");
        PageRequest request = PageRequest.of(query.getPageIndex() - 1, query.getPageSize(),sort);
        Specification<Quality> specification = new Specification<Quality>() {
            @Override
            public Predicate toPredicate(Root<Quality> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<Predicate>();
                if(query.getName() != null && query.getName().trim().length() > 0){
                    predicates.add(criteriaBuilder.like(root.get("name"),"%" + query.getName() + "%"));
                }
                if(query.getCcode() != null && query.getCcode().trim().length() > 0){
                    predicates.add(criteriaBuilder.like(root.get("ccode"),"%" +query.getCcode() + "%"));
                }
                if(query.getCinvcode() != null && query.getCinvcode().trim().length() > 0){
                    predicates.add(criteriaBuilder.like(root.get("cinvcode"),"%" +query.getCinvcode() + "%"));
                }
                if(query.getZjStatus() != null && query.getZjStatus().trim().length() > 0){
                    predicates.add(criteriaBuilder.equal(root.get("zjStatus"),query.getZjStatus()));
                }
                if(query.getSddate() != null){
                    predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createTime"),query.getSddate()));
                }
                if(query.getEddate() != null){
                    predicates.add(criteriaBuilder.lessThan(root.get("createTime"), DateUtils.addDay(query.getEddate())));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return qualityRepository.findAll(specification,request);
    }
}
